package base_grammar

import "fmt"

func Function_cd() {
	// 函数作为参数传递
	f1("golang", sayHello)

	// 函数作为返回值被返回
	f2 := calOp("+")
	i := f2(1, 2)
	fmt.Printf("i: %v\n", i)
	f2 = calOp("-")
	i = f2(1, 2)
	fmt.Printf("i: %v\n", i)
}

func calOp(operator string) func(int, int) int {
	switch operator {
	case "+":
		return addOp
	case "-":
		return subOp
	default:
		return nil
	}
}

func addOp(a int, b int) int {
	return a + b
}

func subOp(a int, b int) int {
	return a - b
}

func sayHello(name string) {
	fmt.Printf("hello, %s \n", name)
}

func f1(name string, f func(string)) {
	f(name)
}
